#ifndef PACKETPARSER_H
#define PACKETPARSER_H

#include <QByteArray>
#include <functional>


class PacketParser
{
public:
    using OnDataReceivedCallback = std::function<void (QByteArray&)>;

private:
    constexpr static const char LEN_PREFIX = 0xFE;
    constexpr static const char LEN_SUFFIX = 0xAD;

    enum ParserStatus {
        STATUS_FIND_PREFIX = 0,
        STATUS_GET_LEN,
        STATUS_FIND_SUFFIX,
        STATUS_RECV_DATA,
        STATUS_CHECKSUM
    };

    ParserStatus mStatus;
    quint8 mLen;
    quint8 mChecksum;
    OnDataReceivedCallback mOnDataReceived;

    QByteArray mDecodedBuff;

public:
    explicit PacketParser(OnDataReceivedCallback onDataReceived);
    virtual ~PacketParser();

    void parser(QByteArray& buff);
    static QByteArray encode(QByteArray& data);
    static quint8 checksum(QByteArray& data);
};

#endif // PACKETPARSER_H
